const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

async function testInventoryOverview() {
  try {
    // 查询所有有效商品及其库存信息
    const products = await prisma.product.findMany({
      where: { status: true },
      include: {
        category: {
          select: { id: true, name: true }
        },
        inventoryRecords: {
          include: {
            warehouse: {
              select: { id: true, name: true }
            }
          }
        }
      }
    });
    
    // 统计预警和缺货商品数量
    let warningProducts = 0;
    let outOfStockProducts = 0;
    
    products.forEach(product => {
      if (product.stockStatus === 'warning') {
        warningProducts++;
      } else if (product.stockStatus === 'out_of_stock') {
        outOfStockProducts++;
      }
    });
    
    // 创建API响应结构
    const response = {
      success: true,
      data: {
        products,
        totalProducts: products.length,
        warningProducts,
        outOfStockProducts
      }
    };
    
    console.log('API响应数据摘要:');
    console.log(`总商品数: ${response.data.totalProducts}`);
    console.log(`预警商品数: ${response.data.warningProducts}`);
    console.log(`缺货商品数: ${response.data.outOfStockProducts}`);
    
    // 保存完整的响应到文件
    const fs = require('fs');
    fs.writeFileSync('inventory-overview-response.json', JSON.stringify(response, null, 2));
    console.log('完整响应已保存到 inventory-overview-response.json');
  } catch (error) {
    console.error('测试库存概览API失败:', error);
  } finally {
    await prisma.$disconnect();
  }
}

testInventoryOverview(); 